let prop_equal = true;
export default {
    /**
    * @description 判断元素是否为字符串
    * @param {*} source
    * @return {Boolen}
    */
    isString(source) {
        return typeof source === 'string'
    },

    /**
     * @description 判断元素是否为数组
     * @param {*} source
     * @return {Boolen}
     */
    isArray(source) {
        if (Array.isArray) return Array.isArray(source)
        else return source instanceof Array
    },

    /**
     * @description 判断元素是否为对象
     * @param {*} source
     * @return {Boolen}
     */
    isObject(source) {
        return Object.prototype.toString.call(source) === '[object Object]'
    }
    ,
    /**
     * @description 判断元素是否为函数
     * @param {*} source
     * @return {Boolen}
     */
    isFunction(source) {
        return typeof source === 'function'
    },

    /**
     * @description 判断元素是否为空
     * @param {*} source
     * @return {Boolen}
     */
    isEmpty(source) {
        if (source === undefined || source === null) return true
        if (this.isString(source) || this.isArray(source)) return source.length === 0
        if (this.isObject(source)) return JSON.stringify(source) === '{}'
        else return source.toString().length === 0
    },

    /**
     * @description 遍历数组、对象
     * @param {*} source 对象或数组
     * @param {Function} func 执行函数，function(i, item) 或 function(key, value)。执行函数返回 false 时，循环终止。
     */
    forEach(source, func) {
        if (source === undefined || source === null) return
        if (typeof (func) !== 'function') return

        let flag
        if (this.isObject(source)) {
            for (let key in source) {
                if (Object.prototype.hasOwnProperty.call(source, key)) {
                    flag = func.apply(window, [key, source[key]])
                    if (flag === false) break
                }
            }
        }
        else {
            for (let i = 0, len = source.length; i < len; i++) {
                flag = func.apply(window, [i, source[i]])
                if (flag === false) break
            }
        }
    },

    /**
     * @description 判断两个元素是否相等
     * @param {*} source1
     * @param {*} source2
     * @param {Boolean} ignoreCase 是否忽略掉大小写，不传则为false
     * @param {Boolean} ignoreSort 是否忽略排序，不传则为false
     * @return {Boolean} 是否相等
     */
    equal(source1, source2, ignoreCase, ignoreSort) {

        // 同为数组或同为对象
        if ((this.isArray(source1) && this.isArray(source2)) || (this.isObject(source1) && this.isObject(source2))) {
            if (this.isArray(source1)) {
                if (source1.length !== source2.length) {
                    prop_equal = false
                    return false
                }
                if (ignoreSort) {
                    source1.sort()
                    source2.sort()
                }
            }
            else {
                if (this.length(source1) !== this.length(source2)) {
                    prop_equal = false
                    return false
                }
            }

            this.forEach(source1, function (ikey, item) {
                return this.equal(item, source2[ikey], ignoreCase, ignoreSort)
            })
            return prop_equal
        }
        // 字符串
        else {
            if (ignoreCase) {
                source1 = String.prototype.toLowerCase.call(source1.toString())
                source2 = String.prototype.toLowerCase.call(source2.toString())
            }
            if (source1 !== source2) prop_equal = false
            return prop_equal
        }
    },


    /**
     * @description 判断元素的长度
     * @param {*} source
     * @return {Number}
     */
    length(source) {
        if (source === undefined || source === null) return 0
        if (this.isString(source) || this.isArray(source)) return source.length
        if (this.isObject(source)) {
            let len = 0
            this.forEach(source, function (key, value) {
                len++
            })
            return len
        }
    }

}